package core;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.NotSerializableException;
import java.util.Vector;

import javax.swing.*;

import utils.FileIO;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class CodexGUI extends javax.swing.JFrame implements ActionListener
{
	private Vector<Unit> unitVector = new Vector<Unit>();
	private JTabbedPane jTabbedPane1;
	private JPanel squadPanel;
	private JPanel walkerPanel;
	private JButton addWalker = new JButton("Add Walker");
	private JLabel wName = new JLabel("Name:");
	private JLabel wWS = new JLabel("WS:");
	private JButton oAdd;
	private JScrollPane jScrollPane2;
	private JList optionList;
	private JTextField oNumber;
	private JTextField oCost;
	private JLabel oNumberLabel;
	private JLabel oCostLabel;
	private JLabel oDescriptionLabel;
	private JLabel lblUnitCount;
	private JLabel oTitle;
	private JTextField oDescription;
	private JTextField ArmyName;
	private JLabel lblArmyName;
	private JPanel optionsPanel;
	private JLabel lblUnitCost;
	private JTextField unitCount;
	private JTextField unitCost;
	private JLabel wBS = new JLabel("BS:");
	private JLabel wS = new JLabel("S:");
	private JLabel wFront = new JLabel("Front:");
	private JLabel wSide = new JLabel("Side:");
	private JLabel wRear = new JLabel("Rear:");
	private JLabel wI = new JLabel("I:");
	private JButton loadCodex;
	private JButton saveCodex;
	private JScrollPane jScrollPane1;
	private JList unitList;
	private JLabel wA = new JLabel("A:");
	private JTextField wwName = new JTextField();
	private JTextField wwWS = new JTextField();
	private JTextField wwBS = new JTextField();
	private JTextField wwS = new JTextField();
	private JTextField wwFront = new JTextField();
	private JTextField wwSide = new JTextField();
	private JTextField wwRear = new JTextField();
	private JTextField wwI = new JTextField();
	private JTextField wwA = new JTextField();
	private JPanel vehiclePanel;
	private JButton addVehicle = new JButton("Add Vehicle");
	private JLabel vName = new JLabel("Name:");
	private JLabel vBS = new JLabel("BS:");
	private JLabel vFront = new JLabel("Front:");
	private JLabel vSide = new JLabel("Side:");
	private JLabel vRear = new JLabel("Rear:");
	private JTextField vvName = new JTextField();
	private JTextField vvBS = new JTextField();
	private JTextField vvFront = new JTextField();
	private JTextField vvSide = new JTextField();
	private JTextField vvRear = new JTextField();
	private DefaultListModel listModel;
	private DefaultListModel oListModel;
	private Vector<Option> optionVector = new Vector<Option>();

	/**
	 * Auto-generated main method to display this JFrame
	 * 
	 * @throws UnsupportedLookAndFeelException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException
	{
		UIManager
				.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{

				CodexGUI inst = new CodexGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public CodexGUI()
	{
		super();
		initGUI();
	}

	private void initGUI()
	{
		try
		{
			setTitle("Create Codex");
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setResizable(false);
			{
				jTabbedPane1 = new JTabbedPane();
				getContentPane().add(jTabbedPane1);
				jTabbedPane1.setBounds(12, 12, 480, 195);
				{
					squadPanel = new JPanel();
					{

					}
					walkerPanel = new JPanel();
					{
						addWalkerPanelContents();
					}
					vehiclePanel = new JPanel();
					{
						addVehiclePanelContents();
					}
					jTabbedPane1.insertTab("Squad", null, squadPanel, null, 0);
					jTabbedPane1
							.insertTab("Walker", null, walkerPanel, null, 1);
					walkerPanel.setLayout(null);
					walkerPanel.setPreferredSize(new java.awt.Dimension(475,
							165));
					jTabbedPane1.insertTab("Vehicle", null, vehiclePanel, null,
							2);
					vehiclePanel.setLayout(null);
					vehiclePanel.setPreferredSize(new java.awt.Dimension(480,
							207));
					squadPanel.setBounds(12, 12, 480, 120);
				}

			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(498, 31, 187, 340);
				{
					listModel = new DefaultListModel();
					unitList = new JList(listModel);
					jScrollPane1.setViewportView(unitList);
					unitList.setModel(listModel);
					unitList.setBounds(498, 54, 122, 363);
					unitList.setPreferredSize(new java.awt.Dimension(166, 330));
				}
			}
			{
				saveCodex = new JButton();
				getContentPane().add(saveCodex);
				saveCodex.setText("Save Codex");
				saveCodex.setBounds(498, 377, 183, 25);
				saveCodex.addActionListener(this);
			}
			{
				loadCodex = new JButton();
				getContentPane().add(loadCodex);
				loadCodex.setText("Load Codex");
				loadCodex.setBounds(498, 407, 183, 25);
				loadCodex.addActionListener(this);
			}
			{
				unitCost = new JTextField();
				getContentPane().add(unitCost);
				unitCost.setBounds(429, 263, 57, 28);
			}
			{
				unitCount = new JTextField();
				getContentPane().add(unitCount);
				unitCount.setBounds(429, 291, 57, 28);
			}
			{
				lblUnitCost = new JLabel();
				getContentPane().add(lblUnitCost);
				lblUnitCost.setText("Base Point Cost:");
				lblUnitCost.setBounds(337, 269, 92, 16);
			}
			{
				lblUnitCount = new JLabel();
				getContentPane().add(lblUnitCount);
				lblUnitCount.setText("Base No. of Units:");
				lblUnitCount.setBounds(330, 297, 99, 16);
			}
			{
				optionsPanel = new JPanel();
				getContentPane().add(optionsPanel);
				optionsPanel.setLayout(null);
				optionsPanel.setBounds(12, 213, 306, 232);
				{
					oDescription = new JTextField();
					optionsPanel.add(oDescription);
					oDescription.setBounds(112, 22, 182, 28);
				}
				{
					oTitle = new JLabel();
					optionsPanel.add(oTitle);
					oTitle.setText("Options:");
					oTitle.setBounds(6, 6, 46, 16);
				}
				{
					oDescriptionLabel = new JLabel();
					optionsPanel.add(oDescriptionLabel);
					oDescriptionLabel.setText("Description:");
					oDescriptionLabel.setBounds(46, 28, 66, 16);
				}
				{
					oCostLabel = new JLabel();
					optionsPanel.add(oCostLabel);
					oCostLabel.setText("Cost Per:");
					oCostLabel.setBounds(61, 56, 51, 16);
				}
				{
					oNumberLabel = new JLabel();
					optionsPanel.add(oNumberLabel);
					oNumberLabel.setText("Number Available:");
					oNumberLabel.setBounds(12, 84, 100, 16);
				}
				{
					oCost = new JTextField();
					optionsPanel.add(oCost);
					oCost.setBounds(112, 50, 182, 28);
				}
				{
					oNumber = new JTextField();
					optionsPanel.add(oNumber);
					oNumber.setBounds(112, 78, 182, 28);
				}
				{
					jScrollPane2 = new JScrollPane();
					optionsPanel.add(jScrollPane2);
					jScrollPane2.setBounds(12, 112, 169, 112);
					{
						oListModel = new DefaultListModel();
						optionList = new JList(oListModel);
						optionList.setModel(oListModel);
						jScrollPane2.setViewportView(optionList);
						optionList.setBounds(6, 117, 288, 109);
						optionList.setPreferredSize(new java.awt.Dimension(146,
								88));
					}
				}
				{
					oAdd = new JButton();
					oAdd.addActionListener(this);
					optionsPanel.add(oAdd);
					oAdd.setText("Add Option");
					oAdd.setBounds(187, 196, 88, 28);
				}
			}
			{
				lblArmyName = new JLabel();
				getContentPane().add(lblArmyName);
				lblArmyName.setText("Army Name:");
				lblArmyName.setBounds(456, 9, 67, 16);
			}
			{
				ArmyName = new JTextField();
				getContentPane().add(ArmyName);
				ArmyName.setBounds(529, 3, 152, 28);
			}
			pack();
			this.setSize(700, 480);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void addWalkerPanelContents()
	{
		int x = -20;
		int y = 45;
		int y2 = 60;
		walkerPanel.add(addWalker);
		addWalker.setBounds(281, 9, 181, 25);
		addWalker.addActionListener(this);
		walkerPanel.add(wName);
		wName.setBounds(20, 5, 40, 12);
		walkerPanel.add(wwName);
		wwName.setBounds(20, 20, 160, 25);
		walkerPanel.add(wWS);
		wWS.setBounds(28, y, 40, 12);
		walkerPanel.add(wwWS);
		wwWS.setBounds(x + 40, y2, 40, 25);
		walkerPanel.add(wBS);
		wBS.setBounds(70, y, 40, 12);
		walkerPanel.add(wwBS);
		wwBS.setBounds(x + 80, y2, 40, 25);
		walkerPanel.add(wS);
		wS.setBounds(110, y, 40, 12);
		walkerPanel.add(wwS);
		wwS.setBounds(x + 120, y2, 40, 25);
		walkerPanel.add(wFront);
		wFront.setBounds(144, y, 40, 12);
		walkerPanel.add(wwFront);
		wwFront.setBounds(x + 160, y2, 40, 25);
		walkerPanel.add(wSide);
		wSide.setBounds(185, y, 40, 12);
		walkerPanel.add(wwSide);
		wwSide.setBounds(x + 200, y2, 40, 25);
		walkerPanel.add(wRear);
		wRear.setBounds(225, y, 40, 12);
		walkerPanel.add(wwRear);
		wwRear.setBounds(x + 240, y2, 40, 25);
		walkerPanel.add(wI);
		wI.setBounds(272, y, 40, 12);
		walkerPanel.add(wwI);
		wwI.setBounds(x + 280, y2, 40, 25);
		walkerPanel.add(wA);
		wA.setBounds(311, y, 40, 12);
		walkerPanel.add(wwA);
		wwA.setBounds(x + 320, y2, 40, 25);
	}

	private void addVehiclePanelContents()
	{
		int x = -20;
		int y = 45;
		int y2 = 60;
		vehiclePanel.add(addVehicle);
		addVehicle.setBounds(281, 9, 181, 25);
		addVehicle.addActionListener(this);
		vehiclePanel.add(vName);
		vName.setBounds(20, 5, 40, 12);
		vehiclePanel.add(vvName);
		vvName.setBounds(20, 20, 160, 25);
		vehiclePanel.add(vBS);
		vBS.setBounds(x + 40, y, 40, 12);
		vehiclePanel.add(vvBS);
		vvBS.setBounds(x + 40, y2, 40, 25);
		vehiclePanel.add(vFront);
		vFront.setBounds(x + 80, y, 40, 12);
		vehiclePanel.add(vvFront);
		vvFront.setBounds(x + 80, y2, 40, 25);
		vehiclePanel.add(vSide);
		vSide.setBounds(x + 120, y, 40, 12);
		vehiclePanel.add(vvSide);
		vvSide.setBounds(x + 120, y2, 40, 25);
		vehiclePanel.add(vRear);
		vRear.setBounds(x + 160, y, 40, 12);
		vehiclePanel.add(vvRear);
		vvRear.setBounds(x + 160, y2, 40, 25);
	}

	public void clearComponents()
	{
		for (Component e : vehiclePanel.getComponents())
		{
			if (e instanceof JTextField)
			{
				((JTextField) e).setText("");
			}
		}
		for (Component e : walkerPanel.getComponents())
		{
			if (e instanceof JTextField)
			{
				((JTextField) e).setText("");
			}
		}
		for (Component e : squadPanel.getComponents())
		{
			if (e instanceof JTextField)
			{
				((JTextField) e).setText("");
			}
		}
		for (Component e : optionsPanel.getComponents())
		{
			if (e instanceof JTextField)
			{
				((JTextField) e).setText("");
			}
		}
		oListModel.clear();
		optionVector.clear();
	}

	public void actionPerformed(ActionEvent ae)
	{
		if (ae.getSource() == addVehicle)
		{

			try
			{
				UVehicle tempV = new UVehicle(vvName.getText(), Integer
						.parseInt(vvBS.getText()), Integer.parseInt(vvFront
						.getText()), Integer.parseInt(vvSide.getText()),
						Integer.parseInt(vvRear.getText()), Integer
								.parseInt(unitCount.getText()), Integer
								.parseInt(unitCost.getText()), "type",
						optionVector);
				listModel.addElement(vvName.getText());
				unitVector.add(tempV);
				clearComponents();
			}
			catch (NumberFormatException ex)
			{
				JOptionPane
						.showMessageDialog(
								null,
								"Please ensure all fields have valid input and try again.",
								"Invalid Input", 1);
			}

		}
		else if (ae.getSource() == addWalker)
		{
			try
			{
				listModel.addElement(wwName.getText());
				UWalker tempW = new UWalker(wwName.getText(), Integer
						.parseInt(wwWS.getText()), Integer.parseInt(wwBS
						.getText()), Integer.parseInt(wwS.getText()), Integer
						.parseInt(wwFront.getText()), Integer.parseInt(wwSide
						.getText()), Integer.parseInt(wwRear.getText()),
						Integer.parseInt(wwI.getText()), Integer.parseInt(wwA
								.getText()), Integer.parseInt(unitCount
								.getText()), Integer.parseInt(unitCost
								.getText()), "type", optionVector);
				unitVector.add(tempW);
				clearComponents();
			}

			catch (NumberFormatException ex)
			{
				JOptionPane
						.showMessageDialog(
								null,
								"Please ensure all fields have valid input and try again.",
								"Invalid Input", 1);
			}
		}
		else if (ae.getSource() == saveCodex)
		{
			if (ArmyName.getText() != "")
			{
				try
				{
					FileIO.writeObject(
							new Codex(ArmyName.getText(), unitVector),
							"test.w40k");
				}
				catch (NotSerializableException e)
				{
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else
			{
				JOptionPane
						.showMessageDialog(
								null,
								"Please ensure you have entered an army name and try again.",
								"Invalid Input", 1);
			}
		}
		else if (ae.getSource() == loadCodex)
		{

		}
		else if (ae.getSource() == oAdd)
		{
			oListModel.addElement(oDescription.getText());
			Option tempO = new Option(oDescription.getText(), Integer
					.parseInt(oCost.getText()), Integer.parseInt(oNumber
					.getText()));
			optionVector.add(tempO);

		}
	}
}
